﻿using AJWPFAdmin.Core;
using AJWPFAdmin.Core.Components.AJTable.ViewModels;
using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services.AppointAPI;
using AJWPFAdmin.Services.EF;
using EntranceGuardManager.Modules.Main.Views;
using Masuit.Tools.Models;
using MaterialDesignExtensions.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using static AJWPFAdmin.Core.ExceptionTool;

namespace EntranceGuardManager.Modules.Main.ViewModels
{
    public class EmergencyFileListViewModel : BindableBase, INavigationAware
    {
        private AJTableFetchOptions _fetchOptions;

        public AJTableFetchOptions FetchOptions
        {
            get
            {
                return _fetchOptions;
            }
            set
            {
                SetProperty(ref _fetchOptions, value);
            }
        }

        private List<AJTableColumnItem> _columns;

        public List<AJTableColumnItem> Columns
        {
            get { return _columns; }
            set { SetProperty(ref _columns, value); }
        }

        private AJTableSearchFormConfig _formConfig;

        public AJTableSearchFormConfig FormConfig
        {
            get { return _formConfig; }
            set { SetProperty(ref _formConfig, value); }
        }

        private DelegateCommand<DataGridRow> _loadingRowCmd;
        public DelegateCommand<DataGridRow> LoadingRowCmd =>
            _loadingRowCmd ?? (_loadingRowCmd = new DelegateCommand<DataGridRow>(ExecuteLoadingRowCmd));

        void ExecuteLoadingRowCmd(DataGridRow row)
        {

        }

        private DelegateCommand<EmergencyFileItem> _downloadCmd;
        public DelegateCommand<EmergencyFileItem> DownloadCmd =>
            _downloadCmd ?? (_downloadCmd = new DelegateCommand<EmergencyFileItem>(ExecuteDownloadCmd));

        void ExecuteDownloadCmd(EmergencyFileItem parameter)
        {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(parameter.Url)
            {
                UseShellExecute = true
            });
        }

        private IDialogService _dialogSvc;
        private DbService db;
        private AppointAPISvc _appointAPISvc;

        public EmergencyFileListViewModel(DbService dbIns, 
            AppointAPISvc appointAPISvc,
            IEventAggregator eventAggregator, IDialogService dialogSvc)
        {
            _dialogSvc = dialogSvc;
            _appointAPISvc = appointAPISvc;
            //eventAggregator.GetEvent<AJGlobalExceptionResolvedEvent>().Subscribe(() => Loading = false);

            FetchOptions = new AJTableFetchOptions
            {
                //ExportFileName = "车辆记录导出",
                Handle = async (formCfg, pageCfg) =>
                {
                    var result = await _appointAPISvc.API.DownLoadEmergencyFileAsync();

                    if (!result.Success)
                    {
                        ExceptionTool.FriendlyError(result.Message, $"{_appointAPISvc.API.KeyDescription} 应急文件下载接口提示");
                    }

                    return result.Data as AJTableFetchResult;
                }
            };

            FormConfig = new AJTableSearchFormConfig
            {
                Schemas = new ObservableCollection<AJTableSearchFormSchema>
                {
                    //new AJTableSearchFormSchema
                    //{
                    //    Labels = new string[]{ "创建开始日期","创建结束日期" },
                    //    Field = nameof(AlarmInfoRecord.CreateDate),
                    //    Type = AJTableSchemaType.RangePicker,
                    //    Value = new ObservableCollection<DateTime?>( new DateTime?[2]{null,null}),
                    //    IsPopular = true,
                    //},
                },
                AdvFilterVisibility = System.Windows.Visibility.Collapsed,
                ExportVisibility = System.Windows.Visibility.Collapsed,
            };

            Columns = new List<AJTableColumnItem>
            {
                //new AJTableColumnItem
                //{
                //    Title = "操作",
                //    CustomTemplate = new AJTableCustomTemplate
                //    {
                //        Key = "TableOperatioin"
                //    },
                //    Fixed = AJTableColumnFixedType.Right,
                //    Width = new DataGridLength(160),
                //},
                new AJTableColumnItem
                {
                    DataIndex = nameof(EmergencyFileItem.Name),
                    Title = "文件名"
                },
                new AJTableColumnItem
                {
                    DataIndex = nameof(EmergencyFileItem.Url),
                    Title = "下载地址",
                    CustomTemplate = new AJTableCustomTemplate
                    {
                        Key = "DownloadFile"
                    }
                },
            };
            db = dbIns;
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //ExecuteGetListCmd();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}
